package com.zhixuan.autoui.sample;

import android.os.Bundle;
import android.os.RemoteException;
import android.widget.EditText;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.tinker.core.aidl.sdk.IMainSdkWeatherCallback;
import com.tinker.core.aidl.sdk.bean.WeatherAirDayBean;
import com.tinker.core.aidl.sdk.bean.WeatherAirDetailBean;
import com.tinker.core.aidl.sdk.bean.WeatherCurBean;
import com.tinker.core.aidl.sdk.bean.WeatherDayBean;
import com.tinker.core.aidl.sdk.bean.WeatherHourBean;
import com.tinker.core.aidl.sdk.bean.WeatherInfoBean;
import com.tinker.core.aidl.sdk.bean.WeatherMinuteBean;
import com.tinker.coresdk.manager.ZXWeatherManager;
import com.tinker.coresdk.utils.LogUtils;
import com.zhixuan.autoui.R;

import java.util.List;

import cd.zhixuan.utils.JsonTools;
import cd.zhixuan.utils.recycler.RecyclerGridSelectTextView;
import cd.zhixuan.utils.recycler.adapter.BaseRecyclerAdapter;
import cd.zhixuan.utils.recycler.adapter.RecyclerListAdapter;
import cd.zhixuan.utils.recycler.adapter.listener.OnBindHolder;
import cd.zhixuan.utils.recycler.adapter.listener.OnItemClickListener;
import cd.zhixuan.utils.recycler.layout_manager.Check;
import cd.zhixuan.utils.recycler.layout_manager.OpenStaggerGridLayoutManager;

/**
 * author: HuZC
 * Date: 2021/12/20
 * Description:
 */
public class WeatherActivity extends BaseTestActivity {
    private static final String TAG = "WeatherActivity";

    private EditText etCityId;
    private RecyclerGridSelectTextView<String> rgstView;

    @Override
    protected int onContentLayout() {
        return R.layout.sample_activity_weather;
    }

    @Override
    protected void onCreated(Bundle savedInstanceState) {

        etCityId = findViewById(R.id.et_city_id);
        rgstView = findViewById(R.id.rgst_view);

//        rgstMusic.getLayoutManager().setSpanCount(2);
        OpenStaggerGridLayoutManager layoutManager = new OpenStaggerGridLayoutManager(6, StaggeredGridLayoutManager.VERTICAL);
        layoutManager.setCanScroll(Check.Y);
        rgstView.setLayoutManager(layoutManager);
        RecyclerListAdapter<String> adapter = rgstView.getAdapter();
        adapter.setDataList(new String[]{
                "实时天气", "7天天气", "多天天气", "24小时天气", "更多小时天气", "分钟级天气",
                "实时空气质量", "每日空气质量"
        });
        adapter.setChoiceMode(BaseRecyclerAdapter.ChoiceMode.NONE);
        adapter.setChoiceState(BaseRecyclerAdapter.ChoiceState.NONE);
        adapter.bindHolder(new OnBindHolder<RecyclerView.ViewHolder, String>() {
            @Override
            public void onBind(RecyclerView.ViewHolder viewHolder, String s, int i) {
                TextView text = viewHolder.itemView.findViewById(R.id.text);
                text.setText(s);
            }
        });
        adapter.setOnItemClickListener(new OnItemClickListener<RecyclerView.ViewHolder, String>() {
            @Override
            public boolean onItemClick(RecyclerView.ViewHolder viewHolder, String s, int i) {
                final String city = etCityId.getText().toString();
                if (city.length() == 0) {
                    toast("请填写城市");
                    return false;
                }
                switch (s) {
                    case "实时天气":
                        ZXWeatherManager.getInstance().getWeatherNow("", "", city, "武侯区", iMainSdkWeatherCallback);
                        break;
                    case "7天天气":
                        ZXWeatherManager.getInstance().getWeather7Days("中国", "四川省", city, "武侯区", iMainSdkWeatherCallback);
                        break;
                    case "多天天气":
                        ZXWeatherManager.getInstance().getWeatherSeveralDays("中国", "四川省", city, "武侯区", iMainSdkWeatherCallback);
                        break;
                    case "24小时天气":
                        ZXWeatherManager.getInstance().getWeather24Hours("中国", "四川省", city, "武侯区", iMainSdkWeatherCallback);
                        break;
                    case "更多小时天气":
                        ZXWeatherManager.getInstance().getWeatherSeveralHours("中国", "四川省", city, "武侯区", iMainSdkWeatherCallback);
                        break;
                    case "分钟级天气":
                        ZXWeatherManager.getInstance().getMinutely("中国", "四川省", city, "武侯区", iMainSdkWeatherCallback);
                        break;
                    case "实时空气质量":
                        ZXWeatherManager.getInstance().getAirNow("中国", "四川省", city, "武侯区", iMainSdkWeatherCallback);
                        break;
                    case "每日空气质量":
                        ZXWeatherManager.getInstance().getAirSeveralDays("中国", "四川省", city, "武侯区", iMainSdkWeatherCallback);
                        break;
                }
                return false;
            }
        });
    }

    private final IMainSdkWeatherCallback.Stub iMainSdkWeatherCallback = new IMainSdkWeatherCallback.Stub() {
        @Override
        public void onWeatherInfo(WeatherInfoBean weatherInfoBean) throws RemoteException {
            logWeatherInfo(weatherInfoBean);

            StringBuilder builder = new StringBuilder();
            builder.append("code=").append(weatherInfoBean.getCode());
            builder.append("\ncodeText=").append(weatherInfoBean.getCodeText());
            builder.append("\nupdateTime=").append(weatherInfoBean.getUpdateTime());
            builder.append("\nweb=").append(weatherInfoBean.getWeb());
            builder.append("\nsummary=").append(weatherInfoBean.getSummary());

            List<String> sources = weatherInfoBean.getSources();
            builder.append("\nsources=");
            if (sources != null) {
                for (String source : sources) {
                    builder.append(source).append("、");
                }
            }

            WeatherCurBean now = weatherInfoBean.getNow();
            builder.append("\n实时天气=").append(now == null ? null : now.getText());
            WeatherAirDetailBean airNow = weatherInfoBean.getAirNow();
            builder.append("\n实时空气质量=").append(airNow == null ? null : airNow.getCategory());

            List<WeatherAirDayBean> airDayList = weatherInfoBean.getAirDayList();
            builder.append("\n每日空气质量 size :").append(airDayList == null ? 0 : airDayList.size());

            List<WeatherDayBean> dailyList = weatherInfoBean.getDailyList();
            builder.append("\n每日天气 size :").append(dailyList == null ? 0 : dailyList.size());

            List<WeatherHourBean> hourlyList = weatherInfoBean.getHourlyList();
            builder.append("\n每时天气 size :").append(hourlyList == null ? 0 : hourlyList.size());

            List<WeatherMinuteBean> minutelyList = weatherInfoBean.getMinutelyList();
            builder.append("\n每分钟降雨 size :").append(minutelyList == null ? 0 : minutelyList.size());

            List<WeatherAirDetailBean> airNowStationList = weatherInfoBean.getAirNowStationList();
            builder.append("\n多站点空气质量 size :").append(airNowStationList == null ? 0 : airNowStationList.size());
            addMessage(builder.toString());
        }

        @Override
        public void onError(int i, String s) throws RemoteException {
            LogUtils.tagLog(TAG, "onError code=" + i + "\n" + s);
        }
    };

    private void logWeatherInfo(WeatherInfoBean bean) {
//        if (bean == null) return;
//        StringBuilder builder = new StringBuilder("WeatherInfoBean:");
//        builder.append("\ncode=").append(bean.getCode());
//        builder.append("\ncodeText=").append(bean.getCodeText());
//        builder.append("\nupdateTime=").append(bean.getUpdateTime());
//        builder.append("\nweb=").append(bean.getWeb());
//        builder.append("\nsources:");
//        List<String> sources = bean.getSources();
//        if (sources != null) {
//            for (int i = 0; i < sources.size(); i++) {
//                String s = sources.get(i);
//                builder.append("\n\t").append(i).append(bean.getWeb());
//            }
//        }
//        WeatherCurBean now = bean.getNow();
//        builder.append("\nnow=").append(now == null ? null : now.toString());
//
//        List<WeatherDayBean> dailyList = bean.getDailyList();
//        builder.append("\ndailyList size=").append(dailyList == null ? 0 : dailyList.size());
//
//        List<WeatherHourBean> hourlyList = bean.getHourlyList();
//        builder.append("\nhourlyList size=").append(hourlyList == null ? 0 : hourlyList.size());
//
//        List<WeatherMinuteBean> minutelyList = bean.getMinutelyList();
//        builder.append("\nminutelyList size=").append(minutelyList == null ? 0 : minutelyList.size());

        LogUtils.tagLog(TAG, JsonTools.object2Json(bean, null));
    }
}
